package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetMealMapper setMealMapper;

    @Autowired
    private SetMealDishMapper setMealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setMealMapper.insert(setmeal);

        Long setMealId = setmeal.getId();
        List<SetmealDish> setMealDishes = setmealDTO.getSetmealDishes();
        setMealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setMealId));
        setMealDishMapper.insert(setMealDishes);
    }

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setMealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除套餐
     *
     * @param setMealIds
     */
    @Override
    public void deleteBatch(List<Long> setMealIds) {
        //起售中的套餐不可删除
        setMealIds.forEach(setMealId -> {
            Setmeal setMeal = setMealMapper.getById(setMealId);
            if (setMeal.getStatus().equals(StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        //删除套餐
        setMealMapper.deleteBatch(setMealIds);
        //删除套餐菜品关联的数据
        setMealDishMapper.deleteBatch(setMealIds);
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setMealMapper.update(setmeal);
        List<SetmealDish> setMealDishes = setmealDTO.getSetmealDishes();
        setMealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDTO.getId()));
        setMealDishMapper.deleteBySetMealId(setmealDTO.getId());
        setMealDishMapper.insert(setMealDishes);
    }

    /**
     * 根据id查询套餐以及相关菜品
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        SetmealVO setMeal = setMealMapper.getByIdEX(id);
        BeanUtils.copyProperties(setMeal, setmealVO);
        List<SetmealDish> setMealDishes = setMealDishMapper.getSetMealDishesBySetMealId(id);
        setmealVO.setSetmealDishes(setMealDishes);
        return setmealVO;
    }

    /**
     * 设置套餐起售停售状态
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //判断该套餐下有没有停售的菜品
        List<SetmealDish> setmealDishList = setMealDishMapper.getSetMealDishesBySetMealId(id);
        if (status.equals(StatusConstant.ENABLE)) {
            setmealDishList.forEach(setmealDish -> {
                Long dishId = setmealDish.getDishId();

                Integer dishStatus = dishMapper.getById(dishId).getStatus();
                if (Objects.equals(dishStatus, StatusConstant.DISABLE)){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }

        //封装更新
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setMealMapper.update(setmeal);
    }

    /**
     * 根据分类id查询起售中的套餐
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> getOnSaleByCategoryId(Long categoryId) {
        return setMealMapper.getOnSaleByCategoryId(categoryId);
    }

    /**
     * 根据套餐id查询关联的菜品
     * @param id
     * @return
     */
    @Override
    public List<DishVO> getDishesById(Long id) {
        List<DishVO> dishVOList = new ArrayList<>();
        List<SetmealDish> setMealDishes = setMealDishMapper.getSetMealDishesBySetMealId(id);
        setMealDishes.forEach(setmealDish -> {
            Dish dish = dishMapper.getById(setmealDish.getDishId());
            DishVO dishVO = DishVO.builder()
                    .name(setmealDish.getName())
                    .copies(setmealDish.getCopies())
                    .image(dish.getImage())
                    .description(dish.getDescription())
                    .build();
            dishVOList.add(dishVO);
        });
        return dishVOList;
    }
}
